package com.i2works.smartluolong.manage.controller.user;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.base.URLConstant;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.web.entity.user.UserRoleDO;
import com.i2works.smartluolong.web.entity.user.UserRoleQuery;
import com.i2works.smartluolong.web.service.user.IUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 会员角色
 *
 * @author Liu Yutao
 * @className UserRoleController
 * @email koal@vip.qq.com
 * @date 2016/2/21 11:30
 */

@Controller
@RequestMapping("/userRole")
public class UserRoleController extends BaseModule {

    @Autowired
    private IUserRoleService userRoleService;

    /**
     * 会员角色管理页面
     */
    @GetMapping(value = "/view/index")
    public String index() {
        logger.info("会员角色管理页面");
        return URLConstant.User.ROLE_URL;
    }

    /**
     * 角色列表
     */
    @GetMapping(value = "/view/api")
    @ResponseBody
    public Result queryUserRole(@ModelAttribute UserRoleQuery query) {
        Result result = new Result();
        try {
            query.setIsPage(true);//启用分页
            int total = userRoleService.queryCount(query);
            List<UserRoleDO> queryUserRoleList = Lists.newArrayList();
            if (total > 0) {
                queryUserRoleList = userRoleService.queryList(query);
            }
            result.setTotal(total);
            result.setRows(queryUserRoleList);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 会员下拉角色列表
     */
    @GetMapping(value = "/view/roleSelect")
    @ResponseBody
    public Result queryUserRoleSelect(@ModelAttribute UserRoleQuery query) {
        Result result = new Result();
        try {
            query.setStatus(1);//只显示启用的
            query.setLimit(100L);
            List<UserRoleDO> queryUserRoleList = userRoleService.queryList(query);
            result.setRows(queryUserRoleList);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 新增编辑角色跳转
     */
    @GetMapping(value = "/save/form")
    public String roleSavePage() {
        logger.info("角色新增编辑");
        return URLConstant.User.ROLE_FORM_URL;
    }


    /**
     * 添加、编辑会员角色
     */
    @PostMapping("/save/action")
    @ResponseBody
    public Result saveUserRole(@ModelAttribute UserRoleDO userRole, @RequestParam(value = "ids", required = false) String ids) {
        Result result = new Result();
        try {
            List<String> resList;
            if (StringUtils.isNotBlank(ids)) {
                resList = BaseUtils.strToList(ids, null);
                if (null != resList && resList.size() > 0) {
                    userRole.setResList(resList);
                }
            }
            userRole.setUpdateName(getUserName());
            userRoleService.save(userRole);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SAVE_ERROR.getMsg());
        }
        return result;
    }


    /**
     * 某个会员角色（角色名和标示）是否存在验证
     */
    @PostMapping("/view/check")
    @ResponseBody
    public boolean queryUserRoleCheck(@ModelAttribute UserRoleQuery query) {
        boolean result = false;
        try {
            int count = userRoleService.queryCheck(query);
            if (count <= 0) {
                result = true;
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return result;
    }

    /**
     * 更新角色状态
     * 备注：附加更新角色与会员关系表
     */
    @PostMapping("/status/action")
    @ResponseBody
    public Result updateUserRoleStatus(@ModelAttribute UserRoleDO userRole) {
        Result result = new Result();
        try {
            userRole.setUpdateName(getUserName());
            userRoleService.updateStatus(userRole);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 批量更新角色状态
     * 备注：附加更新角色与会员关系表
     */
    @PostMapping("/status/batchAction")
    @ResponseBody
    public Result updateUserRoleBatchStatus(@RequestParam("idsList[]") List<Integer> idsList, Integer status) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("idsList", idsList);
                map.put("status", status);
                map.put("updateName", getUserName());
                userRoleService.updateBatchStatus(map);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/delete/action")
    @ResponseBody
    public Result delete(@RequestParam Integer id) {
        Result result = new Result();
        try {
            userRoleService.delete(id);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 批量删除
     */
    @PostMapping("/delete/batchAction")
    @ResponseBody
    public Result deleteBatch(@RequestParam("idsList[]") List<Integer> idsList) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                userRoleService.deleteBatch(idsList);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
        }
        return result;
    }

}
